package com.tarena.lbs.basic.web.service.impl;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.api.UpdateParam;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.repoistory.StoreRepository;
import com.tarena.lbs.basic.web.service.BusinessService;
import com.tarena.lbs.basic.web.service.StoreService;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.param.StoreParam;
import com.tarena.lbs.pojo.basic.query.AreaStoreQuery;
import com.tarena.lbs.pojo.basic.query.StoreQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreRepository storeRepository;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private AttachApi attachApi;

    @Override
    public BasePageList<StoreBO> storeList(StoreQuery query) {
        log.debug("s1-商家列表查询:{}",query);
        return storeRepository.storeList(query);
    }

    @Override
    public void saveStore(StoreParam storeParam)throws BusinessException {
        //1.根据businessId 验证 business是否合法(启用禁用) 是否存在(count(*))
        checkBusiness(storeParam.getBusinessId());
        //2.调用仓储层
        log.debug("S2-新增门店:{}",storeParam);
        Integer storeId=storeRepository.saveStore(storeParam);
        //3.绑定2个业务图片 店铺Logo业务200  店铺图片图片业务 300 TODO
        bindPictures(storeParam,storeId);
    }

    @Override
    public BasePageList<StoreBO> getCitiesStores(AreaStoreQuery query) throws BusinessException{
        //1.获取解析的用户
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        if (userPrinciple==null){
            throw new BusinessException("101","用户解析认证失败");
        }
        //2.查询满足 当前登录用户所属商家范围 提交参数的区域范围的店铺列表
        BasePageList<StoreBO> bos=storeRepository.getStoresByAreasAndBusiness(userPrinciple.getId(),query);
        return bos;
    }

    private void checkBusiness(Integer businessId) throws BusinessException {
        //判断存在不存在
        boolean exists=businessService.existsBusiness(businessId);
        if (!exists){
            throw new BusinessException("105","商家根本不存在,无法新增店铺");
        }
    }

    /**
     * 为商家图片和logo绑定商家id和业务类型
     * @param storeParam
     * @param storeId
     */
    private void bindPictures(StoreParam storeParam, Integer storeId) {
        List<UpdateParam> picUpdateParams=new ArrayList<>();
        //封装 storeParam 传进来的 店铺图片list pidId buzType=300 buzId=storeId
        List<String> storeImagesIds = storeParam.getStoreImagesIds();
        if (!CollectionUtils.isEmpty(storeImagesIds)){
                picUpdateParams=storeImagesIds.stream().map(picId->{
                UpdateParam param=new UpdateParam();
                param.setBusinessType(300);
                param.setBusinessId(storeId);
                param.setId(Integer.valueOf(picId));
                return param;
            }).collect(Collectors.toList());
        }
        //storeParam 提交店铺的入参中 拿到logo图片id
        Integer logoId=Integer.valueOf(storeParam.getStoreLogo());
        UpdateParam logoParam=new UpdateParam();
        logoParam.setId(logoId);
        logoParam.setBusinessType(200);
        logoParam.setBusinessId(storeId);
        picUpdateParams.add(logoParam);
        attachApi.batchUpdateBusiness(picUpdateParams);
    }


}
